package com.zou.szt;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PriceQueryActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private Spinner s1;
    private Spinner s2;
    private Spinner e1;
    private Spinner e2;
    private TextView result;

    int x;
    int countx;
    int y;
    int county;
    private List s2data = new ArrayList();
    private MyAdapter s2adp;
    private List e2data = new ArrayList();
    private MyAdapter e2adp;
    private TextView start;
    private TextView end;
    private RelativeLayout container;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_price_query);
        initView();
        initData();
    }

    private void initView() {

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        s1 = (Spinner) findViewById(R.id.spinner_s1);
        s2 = (Spinner) findViewById(R.id.spinner_s2);
        e1 = (Spinner) findViewById(R.id.spinner_e1);
        e2 = (Spinner) findViewById(R.id.spinner_e2);
        result = (TextView) findViewById(R.id.tv_result);
        start = (TextView) findViewById(R.id.tv_start);
        end = (TextView) findViewById(R.id.tv_end);
        container = (RelativeLayout) findViewById(R.id.grid_container);

    }

    private void initData() {
        s1.setAdapter(new MyAdapter(Arrays.asList(Constants.lines)));
        e1.setAdapter(new MyAdapter(Arrays.asList(Constants.lines)));

        s2data.addAll(Arrays.asList(Constants.stations[0]));
        s2adp = new MyAdapter(s2data);
        s2.setAdapter(s2adp);

        e2data.addAll(Arrays.asList(Constants.stations[0]));
        e2adp = new MyAdapter(e2data);
        e2.setAdapter(e2adp);

        s1.setOnItemSelectedListener(this);
        s2.setOnItemSelectedListener(this);
        e1.setOnItemSelectedListener(this);
        e2.setOnItemSelectedListener(this);
    }

    public void query(View view) {
        container.setVisibility(View.VISIBLE);
        start.setText(getStation(x));
        end.setText(getStation(y));

        result.setVisibility(View.VISIBLE);
        result.setText(getPrice(x, y) + "元");
    }

    private static String getPrice(int x, int y) {
        return Constants.prices[x].split(",")[y];
    }

    private static String getStation(int index) {
        if (index < 30) {
            return "一号线" + Constants.stations[0][index] + "站";
        } else if (index < 59) {
            return "二号线" + Constants.stations[1][index - 30] + "站";
        } else if (index < 89) {
            return "三号线" + Constants.stations[2][index - 59] + "站";
        } else if (index < 104) {
            return "四号线" + Constants.stations[3][index - 89] + "站";
        } else {
            return "五号线" + Constants.stations[4][index - 104] + "站";
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.spinner_s1:
                if (s2data != null) {
                    s2data.clear();
                }
                s2data.addAll(Arrays.asList(Constants.stations[position]));
                s2adp.notifyDataSetChanged();
                countx = 0;
                for (int i = 0; i < position; i++) {
                    countx += Constants.stations[i].length;
                }
                x = countx;
                break;
            case R.id.spinner_s2:
                x = countx + position;
                break;
            case R.id.spinner_e1:
                if (e2data != null) {
                    e2data.clear();
                }
                e2data.addAll(Arrays.asList(Constants.stations[position]));
                e2adp.notifyDataSetChanged();
                county = 0;
                for (int i = 0; i < position; i++) {
                    county += Constants.stations[i].length;
                }
                y = county;
                break;
            case R.id.spinner_e2:
                y = county + position;
                break;
            default:
                Log.e("wocao", "onItemSelected: " + position + "__id_1_" + R.id.spinner_s1 + "_now" + parent.getId() + " id " + id);
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    class MyAdapter extends ArrayAdapter {

        public MyAdapter(Context context, int resource, Object[] objects) {
            super(context, resource, objects);
        }

        public MyAdapter(Context context, int resource, List objects) {
            super(context, resource, objects);
        }

        public MyAdapter(List data) {
            this(PriceQueryActivity.this, android.R.layout.simple_spinner_dropdown_item, data);
        }


    }
}
